/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * V3AcontCrearNuevo.java
 *
 * Created on 14-may-2012, 12:46:29
 */
package practtica4;

import BBDD.GestorBBDD;
import MetodosAuxiliares.MetAux;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author usuario_local
 */
public class V3AcontCrearNuevo extends javax.swing.JFrame {

    MetAux metAux=new MetAux();
    GestorBBDD gestor= new GestorBBDD();
    int iddoc;
    /** Creates new form V3AcontCrearNuevo */
    public V3AcontCrearNuevo() {
        initComponents();
    }

    V3AcontCrearNuevo(int iddoc) throws ClassNotFoundException, SQLException {
        this.iddoc=iddoc;

        initComponents();
        botonId.setText(Integer.toString(gestor.numAcont()));
        botonId.setEditable(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botonNombre = new javax.swing.JTextField();
        botonFechaIni = new javax.swing.JTextField();
        botonId = new javax.swing.JTextField();
        botonDescr = new javax.swing.JTextField();
        labelId = new javax.swing.JLabel();
        botonFechaFin = new javax.swing.JTextField();
        labelFechaFin = new javax.swing.JLabel();
        labelFechaIni = new javax.swing.JLabel();
        labelDesc = new javax.swing.JLabel();
        botonAceptar = new javax.swing.JButton();
        labelNombre = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        botonDescr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDescrActionPerformed(evt);
            }
        });

        labelId.setText("id");

        labelFechaFin.setText("Fecha fin");

        labelFechaIni.setText("Fecha inicio");

        labelDesc.setText("descripción");

        botonAceptar.setText("Aceptar");
        botonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAceptarActionPerformed(evt);
            }
        });

        labelNombre.setText("nombre");

        jLabel1.setText("YYYY/MM/DD");

        jLabel2.setText("YYYY/MM/DD");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelId)
                    .addComponent(labelFechaFin)
                    .addComponent(labelFechaIni)
                    .addComponent(labelDesc)
                    .addComponent(labelNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(botonId, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addComponent(botonNombre)
                        .addComponent(botonDescr)
                        .addComponent(botonFechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(botonFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(130, 130, 130))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(273, Short.MAX_VALUE)
                .addComponent(botonAceptar)
                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelId)
                    .addComponent(botonId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(botonNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelDesc)
                    .addComponent(botonDescr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFechaIni)
                    .addComponent(botonFechaIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(labelFechaFin))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(botonFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(2, 2, 2)))
                .addGap(10, 10, 10)
                .addComponent(botonAceptar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void botonDescrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDescrActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_botonDescrActionPerformed

    private void botonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAceptarActionPerformed
        // TODO add your handling code here:
        String id=botonId.getText();
        if(id.isEmpty()||!metAux.isNumeric(id)){
            JOptionPane.showMessageDialog(this, "Rellenar campo id, sólo con números");
        }
        int id2=Integer.parseInt(id);
        String nombre=botonNombre.getText();
        if(nombre.isEmpty())
            JOptionPane.showMessageDialog(this, "Rellenar campo Nombre");
        String desc=botonDescr.getText();
        if(desc.isEmpty())
            JOptionPane.showMessageDialog(this, "Rellenar campo Descripcion");
        String fechaini=botonFechaIni.getText();
        if(fechaini.isEmpty())
            JOptionPane.showMessageDialog(this, "Rellenar campo Fecha inicio");
        String fechafin=botonFechaFin.getText();
        if(fechafin.isEmpty())
            JOptionPane.showMessageDialog(this, "Rellenar campo Fecha fin");
        try {
            gestor.anyadirAcontecimiento(id2,nombre,desc,fechaini,fechafin);
            gestor.anyadirDocTieneAcont(iddoc, id2);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(V3PersCrearNuevo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(V3PersCrearNuevo.class.getName()).log(Level.SEVERE, null, ex);
        }
        V3 v3=new V3(iddoc);
        v3.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_botonAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(V3AcontCrearNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(V3AcontCrearNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(V3AcontCrearNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(V3AcontCrearNuevo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new V3AcontCrearNuevo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAceptar;
    private javax.swing.JTextField botonDescr;
    private javax.swing.JTextField botonFechaFin;
    private javax.swing.JTextField botonFechaIni;
    private javax.swing.JTextField botonId;
    private javax.swing.JTextField botonNombre;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel labelDesc;
    private javax.swing.JLabel labelFechaFin;
    private javax.swing.JLabel labelFechaIni;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelNombre;
    // End of variables declaration//GEN-END:variables
}
